<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="show ? rowObj.T1 : rowObj.F1" :xs="show ? rowObj.T2 : rowObj.F2">
        <workSiteLeft ref="workSiteLeft" :show="show" @showLeft="showLeft" @queryVal="queryVal"/>
      </el-col>
      <el-col :span="show ? rowObj.T3 : rowObj.F3" :xs="show ? rowObj.T4 : rowObj.F4">
        <div class="box">
          <el-button class="box_btn" size="mini" :icon="show ? 'el-icon-s-fold' : 'el-icon-s-unfold'" circle @click="showLeft(show)"></el-button>
          <SysProjectItem v-if="projectFlag" :paramId="paramId" />
          <SysContract v-else :paramId="paramId" :parentId="parentId" />
        </div>
      </el-col>
    </el-row>

    <!-- 添加或修改项目对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属公司ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入所属公司ID" />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目信息" prop="projectInfo">
          <el-input v-model="form.projectInfo" placeholder="请输入项目信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { listSysProject, getSysProject, delSysProject, addSysProject, updateSysProject } from "@/api/system/sysProject";
import workSiteLeft from "@/views/components/workSiteLeft";
import SysContract from "./sysContract.vue";
import SysProjectItem from "./SysProjectItem.vue";

export default {
  name: "SysProjectIndex",
  components: {
    workSiteLeft,
    SysContract,
    SysProjectItem,
  },
  data() {
    return {
      parentId: "",
      paramId: "",
      rowObj: {
        T1: 3,
        T2: 24,
        T3: 21,
        T4: 24,
        F1: 0,
        F2: 0,
        F3: 24,
        F4: 24,
      },
      projectFlag: true,
      show: true,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      dateRange: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目表格数据
      sysProjectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
        projectName: undefined,
        projectInfo: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [
          { required: true, message: "主键项目ID不能为空", trigger: "blur" },
        ],
        deptId: [
          { required: true, message: "所属公司ID不能为空", trigger: "blur" },
        ],
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
        projectInfo: [
          { required: true, message: "项目信息不能为空", trigger: "blur" },
        ],
        delFlag: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    // this.getList();
  },
  methods: {
    // 左侧树点击事件
    showLeft(show) {
      this.show = !show;
    },
    queryVal(val) {
      console.log(
        "__________________________________________val:-------------" +
          JSON.stringify(val)
      );
      // this.znyj_resultList = [];
      this.projectFlag = val.parentId == "101";
      this.paramId = val.id;
      this.parentId = val.parentId;
      // this.queryParams.id = val.id;
      // this.handleQuery();
    },
    /** 查询项目列表 */
    getList() {
      this.loading = true;
      listSysProject(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          console.log(
            "项目——1————————————————————————————" +
              JSON.stringify(response.data.records)
          );
          console.log(
            "项目—2—————————————————————————————" +
              JSON.stringify(response.data.total)
          );
          this.sysProjectList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: undefined,
        deptId: undefined,
        projectName: undefined,
        projectInfo: undefined,
        delFlag: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      // this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.workSiteLeft.resetQuery();
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.projectId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const projectId = row.projectId || this.ids;
      getSysProject(projectId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.projectId != null) {
            updateSysProject(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addSysProject(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const projectIds = row.projectId || this.ids;
      this.$modal
        .confirm('是否确认删除项目编号为"' + projectIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delSysProject(projectIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "zhgd/sysProject/export",
        {
          ...this.queryParams,
        },
        `sysProject_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.box{
  width: 100%;
  height: 100%;
  position: relative;
  margin: -20px 0 0 -16px;
}
.box_btn{
  position: absolute;
  left:20px;
  top: 23px;
}
</style>
